<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="record-list" v-if="list.length>0">
				<view class="list-item" v-for="(item,index) in list" :key="index">
					<view class="inspection-info">巡店时间：{{item.report.patrolStart||''}}~{{item.report.patrolEnd||''}}</view>
					<view class="inspection-info">巡店人：{{item.username||''}}</view>
					<view class="patrolReport-box">	
						<view class="grade">{{item.report.starsScore||0}}（{{item.patrolScore||0}}分）</view>
						<view class="inspection-summarize">
							<view class="title">巡店总结：</view>
							<view class="desc">{{item.report.summary||'加载中...'}}</view>
						</view>
					</view>
					<view class="classify-box" v-if="item.unqualifiedList.length!=0">
						<view class="classify-title">不合格<text class="disqualification">{{getUnqualifiedNum(item.unqualifiedList)}}</text>项</view>
						<view class="inspect-box">
							<view class="inspect-item" v-for="(val,i) in item.unqualifiedList" :key="i">
								<view class="content">{{i+1}}（{{val.originalScore}}分）：{{val.project}}</view>
								<view class="status flexWrap">
									<text>整改记录</text>
									<view class="disqualification" @tap="jumpPage(val,i+1)"
										v-if="val.status==1">
										待整改<uni-icons type="right" size="13" color="rgba(255, 255, 255, 0.5)"></uni-icons>
									</view>
									<view class="orange" @tap="jumpPage(val,i+1)"
										v-if="val.status==2">
										待审核<uni-icons type="right" size="13" color="rgba(255, 255, 255, 0.5)"></uni-icons>
									</view>
									<view class="qualification" @tap="jumpPage(val,i+1)"
										v-if="val.status==3">
										已整改<uni-icons type="right" size="13" color="rgba(255, 255, 255, 0.5)"></uni-icons>
									</view>
								</view>
							</view>
						</view>
					</view>
					
					<view class="classify-box" v-if="item.qualifiedList.length!=0">
						<view class="classify-title">合格<text class="qualification">{{item.qualifiedList.length}}</text>项</view>
						<view class="inspect-box">
							<view class="inspect-item" v-for="(val,k) in item.qualifiedList" :key="k">
								<view class="content">{{item.unqualifiedList.length+k+1}}（{{val.originalScore}}分）：{{val.project}}</view>
								<view class="status flexWrap" v-if="val.recordType==3">
									<text>优化建议</text>
									<view class="other" @tap="goPage(`/shop-inspection/optimizeSuggestion?id=${val.id}&series=${item.unqualifiedList.length+k+1}`)">
										查看<uni-icons type="right" size="13" color="rgba(255, 255, 255, 0.5)"></uni-icons>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
			<view class="quesheng" v-if="showEmpty">
				<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
			</view>
		</template>
	</view>
</template>

<script>
	export default{	
		data(){
			return{
				pageOpc:0,
				type:'',
				list:[],
				
				page: 1,
				size: 8,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
			}
		},
		onReady() {
			setTimeout(() => {
				this.pageOpc = 1
			}, 100)
		},
		onLoad(options){
			this.type=options.type
		},
		onShow() {
			this.freshList()
		},
		onReachBottom() {
			if (this.more) {
				this.getList()
			}
		},
		methods:{
			freshList(){
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.list = []
				this.getList();
			},
			
			// 分页获取巡店报告列表
			getList(){
				this.$request.post(this.$api.reportList, {
					storeId:uni.getStorageSync('store_id'),
					pageNum:this.page++,  //当前页数
					pageSize:this.size,  //当前条数
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;
						let newArray = data.list
						if(newArray&&newArray.length>0){
							newArray.forEach(item=>{
								[item.unqualifiedList, item.qualifiedList] = this.partitionArray(item.reportProjectList, val => val.status)
							})
						}
						
						if (newArray&&newArray.length == 0 && this.page == 2) {
							this.showEmpty = true
							this.showLoadStatus = false
							this.more = false // 不能再次请求了
							return false;
						} else {
							this.showLoadStatus = true
							this.more = true
							this.showEmpty = false
							this.status = 'loading'
						}
						if (newArray&&newArray.length < this.size) { // 说明没有更多数据
							this.status = 'noMore'
							this.more = false
							this.showEmpty = false
						}
						this.list = this.list.concat(newArray);
						// console.log(this.list)
					}
				})
			},
			
			// 筛选数据
			partitionArray(arr, predicate) {
				return arr.reduce((acc, val) => {
					const partition = predicate(val) ? 0 : 1;
					acc[partition].push(val);
					return acc;
				}, [[],[]]);
			},
			
			// 获取不合格数量
			getUnqualifiedNum(arr){
				return arr.filter(item => item.status!=3).length
			},
			
			jumpPage(val,i){
				let url=''
				if(this.type=='boss'){
					url='/shop-inspection/bossRectifyRecord?id='+val.id+'&series='+i
				}else{
					url='/shop-inspection/rectifyRecord?id='+val.id+'&series='+i
				}
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-fade-in{
		padding-bottom: 50rpx;
	}
	
	.record-list{
		padding: 28rpx;
	}
	.list-item{
		margin-bottom: 60rpx;
		&:last-child{
			margin-bottom: 0;
		}
		.inspection-info{
			margin-top: 10rpx;
			color: rgba(255, 255, 255, 0.7);
			font-size: 24rpx;
		}
		.patrolReport-box{
			margin-top: 14rpx;
			background: #1D1C5C;
			border-radius: 12rpx ;
			padding: 20rpx;
			.grade{
				font-size: 26rpx;
				color: #FFFFFF;
			}
			
			.inspection-summarize{
				margin-top: 12rpx;
				.title{
					color: rgba(255, 255, 255, 0.5);
					font-size: 24rpx;
				}
				.desc{
					margin-top: 10rpx;
					color: rgba(255, 255, 255, 0.8);
					font-size: 22rpx;
				}
			}
			
		}
		
		.classify-box{
			margin-top: 14rpx;
			background: #1D1C5C;
			border-radius: 12rpx;
			padding: 20rpx;
			.disqualification{
				color: #D54941;
			}
			.qualification{
				color: #2BA471;
			}
			.orange{
				color: #FF8E00;
			}
			.other{
				color: #FFB726;
			}
			
			.classify-title{
				font-weight: bold;
				font-size: 26rpx;
				color: #FFFFFF;	
			}
			
			.inspect-box{
				margin-top: 10rpx;
				.inspect-item{
					padding: 7rpx 0;
					&:last-child{
						border-bottom: none;
					}
					.content{
						font-size: 22rpx;
						color: rgba(255,255,255,0.8);
						line-height: 34rpx;
					}
					.status{
						margin-top: 7rpx;
						font-size: 22rpx;
						color: rgba(255,255,255,0.5);
					}
				}
			}	
		}
	}
	.quesheng{
		margin-top: 30rpx;
		display: flex;
		justify-content: center;
		image{
			width: 476upx;
			height: 280upx;
		}
	}
</style>